package com.leve.control.mapper;

import com.leve.control.domain.Cuser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author whongyu
 * @create by 2019/6/12
 */
@Repository
public interface CuserMapper {

    @Select("<script>" +
            "select `cuser_id`,`mobile`,`createtime`,`name`,`company` from ims_levepower_iot_cuser " +
            "<where>  " +
            "<if test=\"cuserId != null and cuserId != ''\"> and cuser_id = #{cuserId} </if>" +
            "<if test=\"mobile != null and mobile != ''\"> and mobile = #{mobile} </if>" +
            "<if test=\"createTime != null and createTime != ''\"> and createtime = #{createTime} </if>" +
            "<if test=\"name != null and name != ''\"> and name = #{name} </if>" +
            "<if test=\"company != null and company != ''\"> and company = #{company} </if>" +
            "</where>" +
            "<choose>" +
            "    <when test=\"sort != null and sort.trim() != ''\">" +
            "         order by ${sort} ${order}" +
            "    </when>" +
            "    <otherwise>" +
            "         order by cuser_id desc" +
            "    </otherwise>" +
            "</choose>" +
            "</script>")
    @ResultType(Cuser.class)
    List<Cuser> findUser(Map<String,Object> map);

    @Insert("insert into ims_levepower_iot_cuser(`cuser_id`, `mobile`, `createtime`, `name`, `company`) values(#{cuserId}, #{mobile}, #{createTime}, #{name}, #{company})")
    @Options(useGeneratedKeys = true,keyProperty = "cuser_id")
    int add(Cuser cuser);

    @Select("select `cuser_id`,`mobile`,`createtime`,`name`,`company` from ims_levepower_iot_cuser where mobile = #{mobile}")
    Cuser findByMobile(String mobile);

    @Delete("delete from ims_levepower_iot_cuser where cuser_id = #{cuserId}")
    int remove(String cuserId);
}
